package com.aizhixin.lab.course.classes.repository;

import com.aizhixin.lab.course.classes.entity.CourseClass;

import java.util.List;

import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;

public interface CourseClassRepository extends JpaRepository<CourseClass, String> {
	
	public List<CourseClass> findAllByDeleteFlagAndCourseId(Integer deleteFlag,String courseId);

	
	public List<CourseClass> findAllByDeleteFlagAndClassIdNotNull(Integer deleteFlag);
	
	public Long countByCourseIdAndClassIdAndDeleteFlag(String courseId,Long classId,Integer deleteFlag);
	CourseClass findByIdAndDeleteFlag(String id,Integer deleteFlag);
	CourseClass findByCourseIdAndClassIdAndDeleteFlag(String courseId,Long classId,Integer deleteFlag);
	@Modifying
    @Query(value="update `t_course_class` cc set cc.stu_num =(select count(*) from `t_user` u where u.delete_flag = 0 and u.role = 'student' and u.class_id = cc.CLASS_ID) where cc.delete_flag =0 and cc.CLASS_ID is not null ",nativeQuery=true)
    void initClassStuNum();
}
